package com.siping.test;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author YangXu
 * @date 2021/10/27
 * @description: 日志解析，提取关键词
 */
public class CmsLogParser {

    private static final String LOG_PATH = "C:\\Users\\yangxu\\Desktop\\logs-from-eqxiu-vip-cms.txt";
    public static final String WRITE_FILE = "C:\\Users\\yangxu\\Desktop\\logs-parse-result.txt";

    private static final String MID_USER_NOT_EXIST_REG = ">>>>>>>该用户在中台不存在eqxiuUserId=>\\[(.*?)\\]";
    private static final String MID_REL_ACCOUNT_NOT_EXIST_REG = "用户子账号relAccount信息不存在eqxiuUserId=>\\[(.*?)\\]";
    private static final String INIT_ACCOUNT_ERROR = ">>>>>>初始化账号错误userId=>(.*?)\\,";
    private static final String UNION_ID_ERROR_REG = ">>>>>>>unionId格式不正确，不能获取到corpId和corpUserId, unionId=>\\[(.*?)\\]，userId=>\\[(.*?)\\]";

    public static void main(String[] args) {
        parseLog();
    }

    public static void parseLog() {
        long start = System.currentTimeMillis();
        Set<String> midUserNotExistList = new HashSet<>();
        midUserNotExistList.add("========用户在中台不存在=========");
        Set<String> midRelAccountNotExistList = new HashSet<>();
        midRelAccountNotExistList.add("========用户子账号relAccount信息不存在=========");
        Set<String> initAccountErrorList = new HashSet<>();
        initAccountErrorList.add("========初始化账号错误=========");
        Set<String> unionIdErrorList = new HashSet<>();
        unionIdErrorList.add("========unionId格式不正确=========");
        try (InputStream is = new FileInputStream(LOG_PATH);
             BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                if (addLogToList(midUserNotExistList, line, MID_USER_NOT_EXIST_REG)) {
                    line = reader.readLine();
                    continue;
                }
                if (addLogToList(midRelAccountNotExistList, line, MID_REL_ACCOUNT_NOT_EXIST_REG)) {
                    line = reader.readLine();
                    continue;
                }
                if (addLogToList(initAccountErrorList, line, INIT_ACCOUNT_ERROR)) {
                    line = reader.readLine();
                    continue;
                }
                if (addLogToList(unionIdErrorList, line, UNION_ID_ERROR_REG)) {
                    line = reader.readLine();
                    continue;
                }
                line = reader.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        writeToFile(WRITE_FILE, midUserNotExistList);
        writeToFile(WRITE_FILE, midRelAccountNotExistList);
        writeToFile(WRITE_FILE, initAccountErrorList);
        writeToFile(WRITE_FILE, unionIdErrorList);
        long time2 = System.currentTimeMillis() - start;
        System.out.println("读取数据完毕，耗时：" + time2 + "ms");
    }

    private static boolean addLogToList(Set<String> midUserNotExistList, String line, String midUserNotExistReg) {
        String midUserNotExist = pickUserId(line, midUserNotExistReg);
        if (midUserNotExist != null) {
            System.out.println("add " + midUserNotExist);
            midUserNotExistList.add(midUserNotExist);
            return true;
        }
        return false;
    }

    public static String pickUserId(String text, String reg) {
        Pattern pattern = Pattern.compile(reg);
        // 内容 与 匹配规则 的测试
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            int count = matcher.groupCount();
            return matcher.group(count);
        } else {
            return null;
        }
    }

    private static void writeToFile(String writeFilePath, Set<String> testList) {
        try (FileWriter fileWriter = new FileWriter(writeFilePath, true);
             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);) {
            for (String text : testList) {
                bufferedWriter.write(text + "\r\n");
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
